package com.xinyues.gamemanager.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.xinyues.gamemanager.dao.entity.GmAccount;
import com.xinyues.gamemanager.dao.entity.GmMenu;
import com.xinyues.gamemanager.service.CommonService;
import com.xinyues.gamemanager.service.GmAccountService;
import com.xinyues.gamemanager.service.GmMenuService;

@Controller
@RequestMapping("layout")
public class LayoutController {
    HttpSessionCsrfTokenRepository csrfTokenRepository =   new HttpSessionCsrfTokenRepository();
    @Autowired
    private GmMenuService menuService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private GmAccountService accountService;
    
    @GetMapping("toCenter")
    public String toCenter() {
        return "gm/center";
    }

    @GetMapping("toAccount")
    public String toAccount() {
        return "gm/system/account";
    }

    @GetMapping("toMenuManager")
    public String toMenuManager() {
        return "gm/system/menuManager";
    }

    @GetMapping("toPermission")
    public String toPermission() {
        return "gm/system/permission";
    }

    @GetMapping("toGmRole")
    public String toGmRole() {
        return "gm/system/gm-role";
    }

    @GetMapping("toUpdatePass")
    public String toUpdatePass() {
        return "gm/system/updatePass";
    }
    @GetMapping("toRoleManager")
    public String toRoleManager() {
        return "gm/dev/game_role";
    }
    
    @RequestMapping("index")
    public String index(HttpServletRequest request,HttpServletResponse response) {
      
        return "gm/index";
    }
    @RequestMapping("toMain")
    public ModelAndView toMain(HttpServletRequest request,ModelAndView view) {
        view.setViewName("gm/main");
        GmAccount account = accountService.getCurrentAccount();
        if(account != null) {
           request.getSession().setAttribute("loginAccount", account.getUsername()); 
        }
        return view;
    }
    @PostMapping("getMenu")
    @ResponseBody
    public Object getMenuHtml(HttpServletRequest request) {
        GmMenu gmMenu = menuService.getGmMenu();
        GmAccount account = accountService.getCurrentAccount();
        String menuHtml = menuService.getMenuHtml(gmMenu,account);
        return commonService.response(menuHtml);
    }
    

    @RequestMapping("toGmAccount")
    public String toGmAccount(ModelAndView view) {
        return "system/gm-account";
    }
    @GetMapping("exit")
    public String exit() {
        return "gm/index";
    }
}
